package io.bluerain.adbdlistenerauto;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;

import com.litesuits.common.utils.ShellUtil;

import io.bluerain.adbdlistenerauto.entities.StatusInfo;

/**
 * Created by foredawn on 15-9-15.
 * 核心Service
 */
public class CoreService extends Service {

    SharedPreferences preferences;
    private StatusInfo info;

    public enum SwitchStatus {

        NORMAL(0),
        ON(1),
        OFF(2);

        private int value;

        SwitchStatus(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static SwitchStatus get(int value) {
            if (value < 0)
                return null;
            switch (value) {
                case 0:
                    return NORMAL;
                case 1:
                    return ON;
                case 2:
                    return OFF;
                default:
                    return null;
            }
        }
    }

    private SwitchStatus status = SwitchStatus.NORMAL;
    private SwitchStatus bootStatus;
    private CoreBinder binder = new CoreBinder();

    public class CoreBinder extends Binder {

        public SwitchStatus status() {
            return getStatus();
        }

        public void status(SwitchStatus status) {
            setStatus(status);
        }
    }

    public CoreService() {
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        info = (StatusInfo) intent.getExtras().getSerializable(MainActivity.STATUS_INFO);
        return binder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (getStatus() != SwitchStatus.NORMAL) { //如果Service中默认值有变动点击开关，则表示单击开关执行Service
            exe(getStatus());
            return 0;
        } else { //否则是以广播开机自启
            preferences = getSharedPreferences("als", Context.MODE_PRIVATE);
            SwitchStatus saveBootStatus = SwitchStatus.get(preferences.getInt(MainActivity.KEY_BOOT_STATUS, 0));
            switch (saveBootStatus) { //开机自启处理
                case NORMAL:
                    break;
                case ON:
                    if (on() == 0) {
                        Toast.makeText(getApplicationContext(), "adbd服务监听成功", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(getApplicationContext(), "adbd服务监听成功开启失败了~ 貌似出了点差错", Toast.LENGTH_LONG).show();
                    }
                    break;
                case OFF:
                    break;
            }
        }

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void exe(SwitchStatus status) {
        switch (status) {
            case NORMAL:
                //什么也不做(保持)
                break;
            case ON:
                if (on() == 0) {
                    Toast.makeText(getApplicationContext(), "开启成功", Toast.LENGTH_LONG).show();
                } else {

                    Toast.makeText(getApplicationContext(), "开启失败了~ 貌似出了点差错", Toast.LENGTH_LONG).show();
                }
                break;
            case OFF:
                //关闭
                if (off() == 0) {
                    Toast.makeText(getApplicationContext(), "关闭成功", Toast.LENGTH_LONG).show();
                } else {

                    Toast.makeText(getApplicationContext(), "关闭失败了~ 貌似出了点差错", Toast.LENGTH_LONG).show();
                }
                break;
        }
    }

    public SwitchStatus getStatus() {
        return status;
    }

    public void setStatus(SwitchStatus status) {
        this.status = status;
    }

    public static int on() {
        //开启
        String[] onCmdList = new String[]{
                "setprop service.adb.tcp.port 5555",
                "stop adbd",
                "start adbd"
        };
        return ShellUtil.execCommand(onCmdList, true, true).result;
    }

    public static int off() {
        String offCmd = "stop adbd";
        return ShellUtil.execCommand(offCmd, true, true).result;

    }

    public SwitchStatus getBootStatus() {
        return bootStatus;
    }

    public void setBootStatus(SwitchStatus bootStatus) {
        this.bootStatus = bootStatus;
    }
}
